package com.ht.api.call.func.impl;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.exception.HtException;
import com.ht.api.db.util.FunctionUtil;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 合并tag_relation数据
 * @author asq
 * @createTime 2025年2月27日 15:25:57
 */
@Slf4j
public class MergeTagRelationFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "合并tag_relation";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		// 获取参数
		String relationId = param.getStr("relation_id");
		String naturalId = param.getStr("natural_id");
		if (StrUtil.isBlank(relationId) && StrUtil.isBlank(naturalId)) {
			throw new HtException("relation_id和natural_id参数至少要传一个~");
		}
		
		// 获取所有group_id下的数据
		IFunctionCall groupInfoCall = FunctionUtil.getFunction(GroupInfoFunctionCall.class.getSimpleName());
		JSONObject allData = (JSONObject)groupInfoCall.execute(me, JSONUtil.createObj());
		
		// 获取当前relation_id和natural_id下的数据
		IFunctionCall queryTagRelationCall = FunctionUtil.getFunction(QueryTagRelationFunctionCall.class.getSimpleName());
		JSONObject relationData = (JSONObject)queryTagRelationCall.execute(me, param.set("is_call", true));
		
		// 将重复数据打上一个标记：visible：true
		return merge(allData, relationData);
	}

	private JSONObject merge(JSONObject allData, JSONObject relationData) {
	    // 获取两个对象的list数组
	    JSONArray allList = allData.getJSONArray("list");
	    JSONArray relationList = relationData.getJSONArray("list");
	    if (relationList == null || relationList.isEmpty()) {
	    	return allData;
	    }
	    
	    // 遍历relationData中的每个分组
	    for (int i = 0; i < relationList.size(); i++) {
	        JSONObject relationGroup = relationList.getJSONObject(i);
	        String relationGroupId = relationGroup.getStr("group_id");
	        
	        // 在allData中查找对应的分组
	        for (int j = 0; j < allList.size(); j++) {
	            JSONObject allGroup = allList.getJSONObject(j);
	            if (allGroup.getStr("group_id").equals(relationGroupId)) {
	                // 找到匹配的分组，设置visible
	                allGroup.set("visible", true);
	                
	                // 处理tag_group数组
	                JSONArray allTagGroups = allGroup.getJSONArray("tag_group");
	                JSONArray relationTagGroups = relationGroup.getJSONArray("tag_group");
	                if (relationTagGroups == null) {
	                	continue;
	                }
	                
	                // 遍历relationData中的每个标签组
	                for (int k = 0; k < relationTagGroups.size(); k++) {
	                    JSONObject relationTagGroup = relationTagGroups.getJSONObject(k);
	                    String relationTagGroupId = relationTagGroup.getStr("tag_group_id");
	                    
	                    // 在allData中查找对应的标签组
	                    for (int l = 0; l < allTagGroups.size(); l++) {
	                        JSONObject allTagGroup = allTagGroups.getJSONObject(l);
	                        if (allTagGroup.getStr("tag_group_id").equals(relationTagGroupId)) {
	                            // 找到匹配的标签组，设置visible
	                            allTagGroup.set("visible", true);
	                            
	                            // 处理tags数组
	                            JSONArray allTags = allTagGroup.getJSONArray("tags");
	                            JSONArray relationTags = relationTagGroup.getJSONArray("tags");
	                            
	                            // 遍历relationData中的每个标签
	                            for (int m = 0; m < relationTags.size(); m++) {
	                                JSONObject relationTag = relationTags.getJSONObject(m);
	                                String relationMainId = relationTag.getStr("main_id");
	                                
	                                // 在allData中查找对应的标签
	                                for (int n = 0; n < allTags.size(); n++) {
	                                    JSONObject allTag = allTags.getJSONObject(n);
	                                    if (allTag.getStr("main_id").equals(relationMainId)) {
	                                        // 找到匹配的标签，设置visible
	                                        allTag.set("visible", true);
	                                        break;
	                                    }
	                                }
	                            }
	                            break;
	                        }
	                    }
	                }
	                break;
	            }
	        }
	    }
	    
	    return allData;
	}

}
